package org.dromara.system.controller.system;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.With;
import org.dromara.system.domain.bo.App.DomainRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SysAppVo;
import org.dromara.system.domain.bo.SysAppBo;
import org.dromara.system.service.ISysAppService;
import org.dromara.common.mybatis.core.page.TableDataInfo;


/**
 * 应用
 *
 * @author X
 * @date 2025-08-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app")
public class SysAppController extends BaseController {

    private final ISysAppService sysAppService;

    /**
     * 查询应用列表
     */
    @SaCheckPermission("system:app:list")
    @GetMapping("/list")
    public TableDataInfo<SysAppVo> list(SysAppBo bo, PageQuery pageQuery) {
        return sysAppService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出应用列表
     */


    @SaCheckPermission("system:                                                                                                                                                                                                                                                                                                                                                                                                                                                             :export")
    @Log(title = "应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAppBo bo, HttpServletResponse response) {
        List<SysAppVo> list = sysAppService.queryList(bo);
        ExcelUtil.exportExcel(list, "应用", SysAppVo.class, response);
    }

    /**
     * 获取应用详细信息
     *
     * @param appId 主键
     */
    @SaCheckPermission("system:app:query")
    @GetMapping("/{appId}")
    public R<SysAppVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long appId) {
        return R.ok(sysAppService.queryById(appId));
    }

    /**
     * 获取应用详细信息
     *
     * @param appId 主键
     */

    @SaIgnore
    @GetMapping("/GetLoginAppInfo/{appId}")
    public R<SysAppVo> GetLoginAppInfo(@NotNull(message = "主键不能为空")@PathVariable long appId) {
        return R.ok(sysAppService.queryById(appId));
    }

    @SaIgnore
    @PostMapping("/GetLoginAppInfoByDomain")
    public R<SysAppVo> GetLoginAppInfoByDomain(@RequestBody DomainRequest request) {
        SysAppBo bo = new SysAppBo();
        bo.setDomain(request.getDomain());
        List<SysAppVo> listApp = sysAppService.queryList(bo);
        SysAppVo vo = new SysAppVo();
        if (listApp != null && !listApp.isEmpty()) {
            vo=listApp.get(0);
        }
        return R.ok(vo);
    }

    /**
     * 新增应用
     */
    @SaCheckPermission("system:app:add")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAppBo bo) {
        return toAjax(sysAppService.insertByBo(bo));
    }

    /**
     * 修改应用
     */
    @SaCheckPermission("system:app:edit")
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAppBo bo) {
        return toAjax(sysAppService.updateByBo(bo));
    }

    /**
     * 删除应用
     *
     * @param appIds 主键串
     */
    @SaCheckPermission("system:app:remove")
    @Log(title = "应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] appIds) {
        return toAjax(sysAppService.deleteWithValidByIds(List.of(appIds), true));
    }
}
